package logic

import (
	"context"
	"fmt"
	"warehouse/model/mysql"

	"warehouse/houserpc/houserpc"
	"warehouse/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *houserpc.AddRequest) (*houserpc.AddResponse, error) {
	// todo: add your logic here and delete this line
	var houses mysql.House
	mysql.DB.Debug().Where("name=?", in.Name).Find(&houses)
	if houses.ID != 0 {
		return nil, fmt.Errorf("该仓库已经存在")
	}

	var house = mysql.House{
		Name:  in.Name,
		Addr:  in.Addr,
		Types: in.Types,
	}
	err := mysql.DB.Debug().Create(&house).Error
	if err != nil {
		return nil, fmt.Errorf("仓库添加失败")
	}

	return &houserpc.AddResponse{Id: int64(house.ID)}, nil
}
